﻿@using Website.CRUDServices
@using Location = Website.Models.Domain.Location
@using Profile = Website.Models.Domain.Profile
@using Region = Website.Models.Domain.Region
@Model
@{
    this.ViewBag.Title = "Nyt Projektmøde";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    var Presentations = this.ViewData["Presentations"] as List<Presentation>;
    var Regions = this.ViewData["Regions"] as List<Region>;
    var Projects = this.ViewData["Projects"] as List<Project>;
    var Demonstrations = this.ViewData["Demonstrations"] as List<ProjectMeetingDemonstration>;
    var Funds = this.ViewData["Funds"] as List<Fund>;
    var Types = this.ViewData["Types"] as List<ProjectType>;
    var Roles = this.ViewData["Roles"] as List<ProjectMeetingRole>;
    var Groups = this.ViewData["Groups"] as List<Group>;
    var Profiles = this.ViewData["Profiles"] as Website.Models.ViewModels.ProfilesView;
    var Project = Model as ProjectMeeting;
    Guid otherId = Guid.Empty;

    // errorhandling
    var errorMsg = new HtmlString(string.Empty);
    if (this.ViewData["ErrorMessage"] != null)
    {
        errorMsg = this.ViewData["ErrorMessage"] as HtmlString;
    }
    bool? isRandD = null;
    if(Types.Any(x => x.ID == Project.Status.Type)){
        isRandD = Types.Single(x => x.ID == Project.Status.Type).IsResearchAndDevelopment;
    }
}
<script type="text/javascript">
    //toggle script
    function toggle(id) {
        var allFields = new Array('NotStarted', 'Started', 'Completed');
        for (var i = 0; i < allFields.length; i++) {
            document.getElementById(allFields[i]).style.display = 'none';
        }

        if (document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == "") {
            document.getElementById(id).style.display = 'block';
        } else {
            document.getElementById(id).style.display = 'none';
        }
    }

    function toggleRD(id) {
        var allFields = new Array('RAndD', 'NotRAndD');
        for (var i = 0; i < allFields.length; i++) {
            document.getElementById(allFields[i]).style.display = 'none';
        }

        if (document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == "") {
            document.getElementById(id).style.display = 'block';
        } else {
            document.getElementById(id).style.display = 'none';
        }
    }
</script>
<script>
    $(function () {
        $("#datepicker").datepicker();
        @if (Project.Status.StatusType == ProjectMeetingStatusType.NotStarted)
        {@Html.Raw("toggle('NotStarted');")}
        @if (Project.Status.StatusType == ProjectMeetingStatusType.Started)
        {@Html.Raw("toggle('Started');")}
        @if (Project.Status.StatusType == ProjectMeetingStatusType.Completed)
        {@Html.Raw("toggle('Completed');")}
        @if (isRandD.HasValue && !isRandD.Value)
        {@Html.Raw("toggleRD('NotRAndD')")}
        @if (isRandD.HasValue && isRandD.Value)
        {@Html.Raw("toggleRD('RAndD')")}
    });
</script>

@using (this.Html.BeginForm())
{
    <div class="error">
        @errorMsg
    </div>

    // ********************ProjectMeeting*********************
    // hidden field to hold ID of the object
    <input type="hidden" id="id" name="id"  readonly="readonly" value="@Project.ID" />
    <input type="hidden" id="statusId" name="statusId"  readonly="readonly" value="@Project.Status.ID" />
    // projects chooser
    <div class="formBullet">Hoved projekt</div>
    <div class="formEntry">
        <select name="TheProject" id="TheProject">
            @foreach (Project proj in Projects)
            {
                if (proj.ID == Project.OwnerID)
                {
                <option value="@proj.ID" selected="selected">@proj.Name</option>
                }
                else
                {
                <option value="@proj.ID">@proj.Name</option>
                }
            }
        </select>
    </div>

            // ownerProjects chooser
    <div class="formBullet">Alle projekter tilknyttet (inkl. hoved projekt)</div>
    <div class="formEntry">
        <select multiple="multiple" name="OwnerProjects" id="OwnerProjects">
            @foreach (Project proj in Projects.Where(x => (Project.OwnerProjects ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@proj.ID" selected="selected">@proj.Name</option>
            }
            @foreach (Project proj in Projects.Where(x => !(Project.OwnerProjects ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@proj.ID">@proj.Name</option>
            }
        </select>
    </div>

            // name
    <div class="formBullet">Navn</div>
    <input type="text" id="Name" name="Name" value="@Project.Name"/>

            // date
    <div class="formBullet">Dato for projekt start</div>
    <input type="text" id="datepicker" name="Date" readonly="readonly" value="@Project.Date" />

            // location
    <div class="formBullet">Lokation</div>
    <div class="formEntry">
        <select name="Location" id="Location">
            <option value="@Guid.Empty">Vælg en lokation</option>
            @foreach (Location loc in Profiles.Locations.OrderBy(x => x.Name))
            {
                if (loc.ID == Project.LocationID)
                {
                <option value="@loc.ID" selected="selected">@loc.Name</option>
                }
                else
                {
                <option value="@loc.ID">@loc.Name</option>
                }
            }
        </select>
        <input type="button" value="Ny" id="newLocation" />
    </div>

            // national or international?
    <div class="formBullet">Hvad er projektet?</div>
    <div>
        <input type="radio" name="IsNational" value="National"@if (Project.IsNational)
                                                              {@Html.Raw("selected='selected'")}>Nationalt
        <input type="radio" name="IsNational" value="International"@if (!Project.IsNational)
                                                                   {@Html.Raw("selected='selected'")}>Internationalt
    </div>
                                                                   // profiles chooser
    <div class="formBullet">Deltagere</div>
    <div class="formEntry">
        <select multiple="multiple" name="Participants" id="Participants">
            @foreach (Profile prof in Profiles.Profiles.Where(x => (Project.Participants ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@prof.ID" selected="selected">@prof.Name</option>
            }
            @foreach (Profile prof in Profiles.Profiles.Where(x => !(Project.Participants ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@prof.ID">@prof.Name</option>
            }
        </select>
    </div>

            // Groups chooser
    <div class="formBullet">Grupper</div>
    <div class="formEntry">
        <select multiple="multiple" name="Groups" id="Groups">
            @foreach (Group group in Groups.Where(x => (Project.Groups ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@group.ID" selected="selected">@group.Name</option>
            }
            @foreach (Group group in Groups.Where(x => !(Project.Groups ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@group.ID">@group.Name</option>
            }
        </select>
    </div>

            // ********************StatusSelector*********************
    <div class="formBullet">Hvad er projektet?</div>
    <div>
        <label>
            <input type="radio" name="StatusType" value="SwitchStatusNotStarted" onclick=" toggle('NotStarted'); " @if (Project.Status.StatusType == ProjectMeetingStatusType.NotStarted)
                                                                                                                   {@Html.Raw("selected='selected'")}>Ikke påbegyndt</label>
        <label>
            <input type="radio" name="StatusType" value="SwitchStatusStarted" onclick=" toggle('Started'); " @if (Project.Status.StatusType == ProjectMeetingStatusType.Started)
                                                                                                             {@Html.Raw("selected='selected'")}>Påbegyndt</label>
        <label>
            <input type="radio" name="StatusType" value="SwitchStatusCompleted" onclick=" toggle('Completed'); " @if (Project.Status.StatusType == ProjectMeetingStatusType.Completed)
                                                                                                                 {@Html.Raw("selected='selected'")}>Færdigt</label>
    </div>
    
                                                                                                                 // ********************StatusNotStarted*********************
    <div class="formEntry open_hidden" id="NotStarted">

        <!--demonstration chooser -->
        <div class="formBullet">Vejledt om</div>
        <div class="formEntry">
            <select multiple="multiple" name="StatusNotStarted.FundsGuidance" id="StatusNotStartedFundsGuidance">
                @foreach (Fund prof in Funds.Where(x => (Project.Status.GuidanceAbout ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID" selected="selected">@prof.Name</option>
                }
                @foreach (Fund prof in Funds.Where(x => !(Project.Status.GuidanceAbout ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID">@prof.Name</option>
                }
            </select>
        </div>
        <!--guidance comments -->
        <div class="formBullet">Kommentarer til vejledning</div>
        <textarea id="StatusNotStartedGuidanceText" name="StatusNotStarted.GuidanceText">@Project.Status.GuidanceAboutText</textarea>

        <!--demonstration chooser -->
        <div class="formBullet">Ansøgninger til</div>
        <div class="formEntry">
            <select multiple="multiple" name="StatusNotStarted.FundsApplication" id="StatusNotStartedFundsApplication">
                @foreach (Fund prof in Funds.Where(x => (Project.Status.Financing ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID" selected="selected">@prof.Name</option>
                }
                @foreach (Fund prof in Funds.Where(x => !(Project.Status.Financing ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID">@prof.Name</option>
                }
            </select>
        </div>
        <!--application comments-->
        <div class="formBullet">Kommentarer til ansøgning</div>
        <textarea id="StatusNotStartedApplicationText" name="StatusNotStarted.ApplicationText">@Project.Status.FinancingText</textarea>

        <!--demonstration chooser -->
        <div class="formBullet">Innovations of idé udvikling</div>
        <div class="formEntry">
            <select multiple="multiple" name="StatusNotStarted.InnovationAndIdeas" id="StatusNotStartedInnovationAndIdeas">
                @foreach (ProjectMeetingDemonstration prof in Demonstrations.Where(x => (Project.Status.DemonstrationIDs ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID" selected="selected">@prof.Name</option>
                }
                @foreach (ProjectMeetingDemonstration prof in Demonstrations.Where(x => !(Project.Status.DemonstrationIDs ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID">@prof.Name</option>
                }
            </select>
        </div>
    </div>

                // ********************StatusStarted*********************
    <div class="formEntry open_hidden" id="Started">
        <!--roles chooser -->
        <div class="formBullet">Roller</div>
        <div class="formEntry">
            <select multiple="multiple" name="StatusStarted.Roles" id="StatusStartedRoles">
                @foreach (ProjectMeetingRole prof in Roles.Where(x => (Project.Status.Roles ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID" selected="selected">@prof.Name</option>
                }
                @foreach (ProjectMeetingRole prof in Roles.Where(x => !(Project.Status.Roles ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID">@prof.Name</option>
                }
            </select>
        </div>

        <!--financing chooser -->
        <div class="formBullet">Financiering</div>
        <div class="formEntry">
            <select multiple="multiple" name="StatusStarted.Financing" id="StatusStartedFinancing">
                @foreach (Fund prof in Funds.Where(x => (Project.Status.Financing ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID" selected="selected">@prof.Name</option>
                }
                @foreach (Fund prof in Funds.Where(x => !(Project.Status.Financing ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID">@prof.Name</option>
                }
            </select>
        </div>
        <!--More Information -->
        <div class="formBullet">Yderligere information om financiering</div>
        <textarea id="StatusStartedFinancingComments" name="StatusStarted.FinancingComments">@Project.Status.FinancingText</textarea>


    </div>

                // ********************StatusCompleted*********************
    <div class="formEntry open_hidden" id="Completed">
        <!--demonstration chooser -->
        <div class="formBullet">Gennemførte ting</div>
        <div class="formEntry">
            <select multiple="multiple" name="StatusCompleted.Demonstration" id="StatusCompletedDemonstration">
                @foreach (ProjectMeetingDemonstration prof in Demonstrations.Where(x => (Project.Status.DemonstrationIDs ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID" selected="selected">@prof.Name</option>
                }
                @foreach (ProjectMeetingDemonstration prof in Demonstrations.Where(x => !(Project.Status.DemonstrationIDs ?? new Guid[0]).Contains(x.ID)))
                {
                    <option value="@prof.ID">@prof.Name</option>
                }
            </select>
        </div>
    </div>

                // ********************R&D chooser*********************    
    <div class="formBullet">R&D type</div>
    <div>
        <label>
            <input type="radio" name="StatusType" value="SwitchStatusNotRAndD" onclick=" toggleRD('NotRAndD'); "  @if (isRandD.HasValue && !isRandD.Value)
                                                                                                             {@Html.Raw("selected='selected'")}>Non R&D</label>
        <label>
            <input type="radio" name="StatusType" value="SwitchStatusRAndD" onclick=" toggleRD('RAndD'); "  @if (isRandD.HasValue && isRandD.Value)
                                                                                                             {@Html.Raw("selected='selected'")}>R&D</label>
    </div>

                // ********************StatusNotRAndD*********************
    <div class="formEntry open_hidden" id="NotRAndD">

        <!--project type -->
        <div class="formBullet">Projekt Type</div>
        <div class="formEntry">
            <select name="StatusNotRAndD.Type" id="StatusNotRAndDType">
                @foreach (ProjectType ty in Types.Where(x => x.IsResearchAndDevelopment == false))
                {
                    if(ty.ID==Project.Status.Type){
                        <option value="@ty.ID" selected="selected">@ty.Name</option>
                    }else{
                        <option value="@ty.ID">@ty.Name</option>
                    }
                }
            </select>
        </div>
        <!--More Information -->
        <div class="formBullet">Yderligere information</div>
        <textarea id="StatusNotRAndDMoreInformation" name="StatusNotRAndD.MoreInformation">@Project.Status.Description</textarea>


    </div>

                // ********************StatusRAndD*********************
    <div class="formEntry open_hidden" id="RAndD">
        <!--project type -->
        <div class="formBullet">Projekt Type</div>
        <div class="formEntry">
            <select name="StatusRAndD.Type" id="StatusRAndDType">
                @foreach (ProjectType ty in Types.Where(x => x.IsResearchAndDevelopment))
                {
                    if(ty.ID==Project.Status.Type){
                        <option value="@ty.ID" selected="selected">@ty.Name</option>
                    }else{
                        <option value="@ty.ID">@ty.Name</option>
                    }
                }
            </select>
        </div>
        <!--More Information -->
        <div class="formBullet">Yderligere information</div>
        <textarea id="StatusRAndDMoreInformation" name="StatusRAndD.MoreInformation">@Project.Status.Description</textarea>

    </div>

    // ********************StatusPatents*********************
    <div class="formEntry" id="Patents">

        <!--patent name -->
        <div class="formBullet">Patent navn</div>
        <input type="text" id="StatusPatentsPatentName" name="StatusPatents.PatentName" value="@Project.Status.PatentName" />
    </div>

    // ********************Submit*********************
    <div class="buttonHolder">
        <input type="submit" value="Opret" />
    </div>
}


<script>

    $(function () {
        $('#StatusNotStartedFundsGuidance').multiSelect();
        $('#StatusNotStartedFundsApplication').multiSelect();
        $('#StatusNotStartedInnovationAndIdeas').multiSelect();
        $('#Participants').multiSelect();
        $('#StatusStartedRoles').multiSelect();
        $('#StatusStartedFinancing').multiSelect();
        $('#StatusCompletedDemonstration').multiSelect();
        $('#Groups').multiSelect();
        $('#OwnerProjects').multiSelect();

        //handle locations
        $("#newLocation").click(function () {
            $("#dialog").dialog({
                width: "340px",
                modal: true
            });
        });
        $('#CreateLocationForm').submit(function (e) {
            e.preventDefault();
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    if (result.success) {
                        // We have a JSON object in case of success
                        $('#Locations')
                            .append($("<option></option>")
                                .attr("value", result.locationID)
                                .text(result.locationName))
                            .val(result.locationID);


                        $("#dialog").dialog('close');
                    } else {
                        // We have the partial with errors in case of failure
                        // so all we have to do is update the DOM
                        $('#CreateLocationForm').html(result);
                    }
                }
            });
        });

    });
</script>

<div id="dialog" title="Opret ny lokation" style="display: none;">
    @Html.Partial("~/Views/Locations/_CreateLocationPartial.cshtml", new Location())
</div>
